[ASP.NET] Change CulturalInfo after button click

Posted by Bart on Stack Overflow See other posts from Stack Overflow or by Bart
Published on 2010-04-03T21:21:02Z Indexed on 2010/04/03 21:23 UTC
Read the original article Hit count: 486

Filed under:
|
|
|

Hello, i have multilingual asp.net site. there is masterpage and default.aspx in masterpage i put two buttons one to click when i want to change the language to english, second for polish. I want to change the language after click on these buttons (and all changes should appear automatically on the page)

here is a code for both:

protected void EnglishButton_Click(object sender, ImageClickEventArgs e)
{
    string selectedLanguage = "en-US";
    //Sets the cookie that is to be used by InitializeCulture() in content page
    HttpCookie cookie = new HttpCookie("CultureInfo");
    cookie.Value = selectedLanguage;
    Response.Cookies.Add(cookie);
    Server.Transfer(Request.Path);
}

protected void PolishButton_Click(object sender, ImageClickEventArgs e)
{
    string selectedLanguage = "pl-PL";
    //Sets the cookie that is to be used by InitializeCulture() in content page
    HttpCookie cookie = new HttpCookie("CultureInfo");
    cookie.Value = selectedLanguage;
    Response.Cookies.Add(cookie);
    Server.Transfer(Request.Path);
}

in default.aspx.cs i have InitializeCulture():

protected override void InitializeCulture()
{
    HttpCookie cookie = Request.Cookies["CultureInfo"];

    // if there is some value in cookie
    if (cookie != null && cookie.Value != null)
    {
        Thread.CurrentThread.CurrentCulture =
            CultureInfo.CreateSpecificCulture(cookie.Value);
        Thread.CurrentThread.CurrentUICulture = new
            CultureInfo(cookie.Value);
    }
    else // if none value has been sent by cookie, set default language
    {
        Thread.CurrentThread.CurrentCulture =
            CultureInfo.CreateSpecificCulture("pl-PL");
        Thread.CurrentThread.CurrentUICulture = new
            CultureInfo("pl-PL");
    }

    base.InitializeCulture();
}

i added resource files and in one label i show actual culture:

Welcome.Text = "Culture: " + System.Globalization.CultureInfo.CurrentCulture.ToString();

the problem is that when i run this app and click e.g. english button (default language is polish), there is no effect. if i click it second time or press F5, the changes are applies and in the label is Culture: en-US. the same happens if i want to change language back to polish (it works after second click (or one click and refresh)).

What am i doing wrong?

Regards, Bart

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about c#